package com.uroad.lib.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.ToggleButton;

import com.uroad.lib.R;

import java.util.ArrayList;
import java.util.List;


public class PointView extends LinearLayout {

    Context mContext;
    RadioGroup rgPoints;
    List<ToggleButton> points;
    int resId = R.drawable.selector_rb_point;

    public PointView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public PointView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        points = new ArrayList<ToggleButton>();
        setGravity(Gravity.CENTER);
    }

    public void setPointImage(int resId) {
        this.resId = resId;
    }

    /**
     * 初始化点
     *
     * @param total ：点的总数
     **/
    public void setPoints(int total) {
        if (total > 0) {
            points.clear();
            removeAllViews();
            for (int i = 0; i < total; i++) {
                ToggleButton tg = new ToggleButton(mContext);
                tg.setBackgroundResource(resId);

                LayoutParams layoutParams = new LayoutParams(
                        20, 20);
                layoutParams.setMargins(5, 0, 5, 0);// 4个参数按顺序分别是左上右下

                tg.setLayoutParams(layoutParams);
                points.add(tg);
                addView(tg);
            }
        }
    }

    /**
     * 设置当前选择的点
     **/
    public void setCurrPoint(int index) {
        if (index < points.size()) {
            for (int i = 0; i < points.size(); i++) {
                if (i != index) {
                    points.get(i).setChecked(false);
                } else {
                    points.get(i).setChecked(true);
                }
            }
        }
    }

}
